<?php

namespace lico\Runtime;

class Request
{

    //server数组
    protected $serverVal;

    //域名
    protected $domainVal;

    //访问路径
    protected $uriVal;

    //请求方式
    protected $styleVal;

    //用户输入路径
    protected $clientPathVal;

    //路径信息
    protected $pathInfoVal;

    //所有参数
    protected $paramsVal;
    
    //模块
    protected $moduleVal;

    //控制器
    protected $controllerVal;

    //版本号
    protected $versionVal;

    //方法
    protected $methodVal;

    //接口路径
    protected $actionVal;

    public function __construct()
    {
        
    }
    
    public function run()
    {
        $this->pathInit();
        return true;
    }

    /**
     * server数组
     */
    public function server()
    {
        if (is_null($this->serverVal)) {
            $this->serverVal = $_SERVER;
        }
        return $this->serverVal;
    }

    /**
     * 域名
     */
    public function domain()
    {
        $this->domainVal = $this->server()['SERVER_NAME'];
        return $this->domainVal;
    }
    
    /**
     * 地址uri
     */
    public function uri()
    {
        $this->uriVal = $this->server()['REQUEST_URI'];
        return $this->uriVal;
    }

    /**
     * 请求方式：post,get
     */
    public function style()
    {
        $this->styleVal = $this->server()['REQUEST_METHOD'];
        return strtoupper($this->styleVal);
    }
    
    /**
     * 所有参数
     */
    public function params()
    {
        $this->paramsVal = array_merge($_GET, $_POST);
        return $this->paramsVal;
    }

    /**
     * 路径初始化
     */
    public function pathInit()
    {

        if (is_null($this->pathInfoVal)) {
            $this->pathInfoVal   = [];
            $this->moduleVal     = constant("SYSCONFIG")['App']['module'];
            $this->controllerVal = ucfirst(constant("SYSCONFIG")['App']['controller']);
            $this->versionVal    = constant("SYSCONFIG")['App']['version'];
            $this->methodVal     = constant("SYSCONFIG")['App']['method'];
      
            $this->uri();
            $uri = $this->uriVal;
            $acturl = $uri;
            if (stripos($uri, 'index.php')) {//是否有index.php
                $acturl = substr($uri, stripos($uri, 'index.php')+10);
            }
            $this->clientPathVal = $acturl;
            if (ltrim($acturl, '/')) {
                $method = $this->methodVal;
                $pathArray = array_values(explode('/', ltrim($acturl, '/')));
                $pathCount = count($pathArray);
                if ($pathCount >= 1) {
                    $this->moduleVal       = $pathArray[0];
                }
                if ($pathCount >= 2) {
                    $this->versionVal   = $pathArray[1];
                }
                if ($pathCount >= 3) {
                    $this->controllerVal   = ucfirst($pathArray[2]);
                }
                if ($pathCount >= 4) {
                    $method             = $pathArray[3];
                }
                
                if (stripos($method, '.')) {
                    $this->methodVal     = substr($method, 0, stripos($method, '.'));
                } else {
                    $this->methodVal     = $method;
                }
            }

            $this->pathInfoVal = [
                'module'=>$this->moduleVal,
                'controller'=>$this->controllerVal,
                'version'=>$this->versionVal,
                'method'=>$this->methodVal,
            ];
            // p($this->pathInfoVal);
            $this->actionVal = $this->moduleVal.'/'.$this->versionVal.'/'.$this->controllerVal.'/'.$this->methodVal;
        }
        
        return $this->pathInfoVal;
    }

    /**
     * 模块
     */
    public function module()
    {
        $this->pathInit();
        return $this->moduleVal;
    }

    /**
     * 控制器
     */
    public function controller()
    {
        $this->pathInit();
        return $this->controllerVal;
    }

    /**
     * 版本
     */
    public function version()
    {
        $this->pathInit();
        return $this->versionVal;
    }

    /**
     * 方法
     */
    public function method()
    {
        $this->pathInit();
        return $this->methodVal;
    }

    /**
     * 接口
     */
    public function action()
    {
        $this->pathInit();
        return $this->actionVal;
    }

    /**
     * 用户输入路径
     */
    public function clientPath()
    {
        $this->pathInit();
        return $this->clientPathVal;
    }

    public function __get($name)
    {
        if (method_exists($this, $name)) {
            return $this->name();
        } else {
            return false;
        }
    }
}
